import { Controller, Post, Get, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiQuery, ApiResponse } from '@nestjs/swagger';
import { SchedulerService } from './scheduler.service';
import { GlobalAuthGuard } from '../../common/guards/global-auth.guard';
import { Roles } from '../../common/decorators/roles.decorator';
import { ApiResponseDto } from '../../common/dto/response.dto';

@ApiTags('定时任务管理')
@Controller('scheduler')
@UseGuards(GlobalAuthGuard)
export class SchedulerController {
  constructor(private readonly schedulerService: SchedulerService) {}

  @Get('status')
  @ApiOperation({ summary: '获取定时任务状态' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getStatus(): Promise<ApiResponseDto> {
    const status = this.schedulerService.getSchedulerStatus();
    return ApiResponseDto.success(status, '获取定时任务状态成功');
  }

  @Post('clean-user-logs')
  @Roles('admin')
  @ApiOperation({ summary: '手动清理用户日志' })
  @ApiQuery({ name: 'days', description: '保留天数', required: false, type: Number })
  @ApiResponse({ status: 200, description: '清理成功' })
  async cleanUserLogs(@Query('days') days?: number): Promise<ApiResponseDto> {
    const retentionDays = days || 90;
    const deletedCount = await this.schedulerService.manualCleanUserLogs(retentionDays);
    
    return ApiResponseDto.success({
      deletedCount,
      retentionDays,
      message: `成功清理了 ${deletedCount} 条用户日志记录`,
    }, '手动清理用户日志成功');
  }

  @Post('clean-security-logs')
  @Roles('admin')
  @ApiOperation({ summary: '手动清理安全日志' })
  @ApiQuery({ name: 'days', description: '保留天数', required: false, type: Number })
  @ApiResponse({ status: 200, description: '清理成功' })
  async cleanSecurityLogs(@Query('days') days?: number): Promise<ApiResponseDto> {
    const retentionDays = days || 90;
    const deletedCount = await this.schedulerService.manualCleanSecurityLogs(retentionDays);
    
    return ApiResponseDto.success({
      deletedCount,
      retentionDays,
      message: `成功清理了 ${deletedCount} 条安全日志记录`,
    }, '手动清理安全日志成功');
  }
}
